<template>
  <div class="search">
    <div class="s-input">
      <div class="input-box">
        <i class="iconfont icon-sousuo"></i>
        <input ref="search" type="text" v-model="keyword" placeholder="搜索歌曲、歌手、专辑" @keyup.enter="search(keyword)"
          @focus="toSearch" />
        <div v-show="keyword" class="icon-clear" @click.stop="keyword = ''"></div>
      </div>
    </div>

    <div class="hot-search" v-show="!keyword">
      <h6>热门搜索</h6>
      <ul class="labels">
        <li v-for="(item, i) in hotWord" :key="i" @click="search(item.first)">
          {{ item.first }}
        </li>
      </ul>

      <h6 v-if="history.length !== 0" style="padding-top: 10px">历史搜索</h6>
      <KeySearch v-for="kw in history" :key="kw" :kw="kw" @searchByClick="searchByClick">
        <template #icon>
          <div class="icon-time icon-s"></div>
        </template>
        <div class="btn-del" @click="delHis"></div>
      </KeySearch>
    </div>

    <div class="keySearch" v-show="keyword && searching">
      <h3>搜索“{{ keyword }}”</h3>
      <KeySearch v-for="(kw, i) in searchKey" :key="i" :kw="kw.keyword" @searchByClick="searchByClick">
        <template #icon>
          <div class="icon-search icon-s"></div>
        </template>
      </KeySearch>
    </div>

    <div v-if="songList.length !== 0" v-show="keyword && !searching">
      <div class="best" v-show="true">
        <h6>最佳匹配</h6>
        <div class="best-part">
          <div class="pic">
            <img :src="imgUrlPadEnd(songList[0]?.al.picUrl)" alt="" />
          </div>
          <div class="desc">
            歌手:
            <span v-for="(ar, i) in songList[0]?.ar" :key="i">{{ ar.name }} {{ i === songList[0].ar.length - 1 ? "" : "/"
                          }}
            </span>
          </div>
          <div class="icon-more">
            <i class="iconfont icon-gengduo-copy"></i>
          </div>
        </div>
      </div>
      <single-song v-for="item in songList" :key="item.id" :item="item" @click.native="
                $emit('change-current-song', { song: item, songList: songList })
              " :song="song" :paused="paused"></single-song>
    </div>
    <div v-else v-show="keyword && !searching" class="without_res">
      暂无搜索结果
    </div>
  </div>
</template>

<script>
import KeySearch from "@/components/KeySearch.vue";
import SingleSong from "@/components/SingleSong.vue";
export default {
  props: ["song", "paused"],

  data() {
    return {
      historyKey: "hisKey",
      keyword: "",
      searching: true,

      // 历史搜索记录
      history: [],

      // 搜索到的歌曲信息
      songList: [],

      // 热门搜索
      hotWord: [],

      // 搜索关键字
      searchKey: [],
    };
  },

  components: {
    KeySearch,
    SingleSong,
  },

  created() {
    this.history = localStorage.getItem(this.historyKey)
      ? JSON.parse(localStorage.getItem(this.historyKey))
      : [];

    // 接口获取热门搜索数据
    this.axios.get("search/hot").then(({ data }) => {
      this.hotWord = data.result.hots;
    });
  },

  watch: {
    history() {
      localStorage.setItem(this.historyKey, JSON.stringify(this.history));
    },

    keyword() {
      if (this.keyword.trim() == "") {
        return;
      }
      this.getSearchLink();
    },
  },

  methods: {
    search(keyword) {
      if (keyword.trim() == "") {
        this.keyword = "";
        return false;
      }
      // 接口获取搜索的歌曲信息
      this.keyword = keyword;
      this.axios
        .get(`/cloudsearch/multimatch?keywords=${keyword}`)
        .then((res) => {
          // console.log(res.data.result.songs)
          this.songList = res.data.result.songs;
        });

      this.history.unshift(this.keyword);
      let set = new Set(this.history);
      this.history = [...set];
      this.searching = false;

      this.$refs.search.blur();
    },

    // 输入框聚焦时，显示搜索匹配字段，隐藏搜索结果
    toSearch() {
      this.searching = true;
    },

    searchByClick(data) {
      // this.keyword = data
      this.search(data);
    },

    delHis(e) {
      let data = e.target.previousSibling.textContent;
      this.history = this.history.filter((o) => o !== data);
    },

    getSearchLink() {
      this.axios
        .get(`search/suggest?keywords=${this.keyword}&type=mobile`)
        .then(({ data }) => {
          // console.log(data.result.allMatch)
          this.searchKey = data.result.allMatch;
        });
    },

    test(dd) {
      console.log(dd);
    },
  },
};
</script>
<style lang='scss'>
$grey: #ebecec;
$blue: #507daf;

.search {
  .icon-s {
    width: 15px;
    height: 15px;
    margin: 0 10px;

    img {
      width: 100%;
    }
  }

  .icon-time {
    background: url("../../images/time.svg") no-repeat;
    background-size: contain;
  }

  .icon-search {
    background: url("../../images/icon-search.svg") no-repeat;
    background-size: contain;
  }
}

.hot-search {
  padding: 15px 10px 0;

  h6 {
    font-size: 12px;
    line-height: 12px;
    color: #666;
    font-weight: 500;
  }

  .labels {
    display: flex;
    flex-wrap: wrap;
    margin-top: 10px;

    li {
      height: 32px;
      padding: 0 14px;
      margin: 0 8px 8px 0;
      border: 1px solid $grey;
      border-radius: 16px;
      text-align: center;
      line-height: 32px;
      font-size: 14px;
      color: #333;
    }
  }
}

.keySearch {
  h3 {
    // height: 50px;
    padding: 10px 0;
    margin-left: 10px;
    border-bottom: 1px solid $grey;
    font-size: 15px;
    font-weight: 500;
    color: $blue;
    line-height: 22px;
  }
}

.best {
  padding: 8px 0 4px;

  h6 {
    font-size: 12px;
    line-height: 12px;
    color: #666;
    font-weight: 500;
  }

  .best-part {
    display: flex;
    align-items: center;
    height: 50px;
    margin-left: 10px;
    padding: 8px 10px 8px 0;
    border-bottom: 1px solid #ddd;

    .pic {
      width: 70px;
      height: 100%;

      img {
        height: 100%;
      }
    }

    .desc {
      flex: 1;
      font-size: 17px;
      color: #333;
    }

    .icon-more {
      height: 100%;
      line-height: 50px;

      i {
        font-size: 24px;
        color: #999;
      }
    }
  }
}

// 搜索框
.van-search {
  border-bottom: 1px solid $grey;
}

.van-search__content {
  background-color: $grey;
}

.s-input {
  height: 60px;
  padding: 15px 10px;
  box-sizing: border-box;

  .input-box {
    display: flex;
    justify-content: space-between;
    align-items: center;

    height: 100%;
    border-radius: 15px;

    background-color: #ebecec;

    input {
      flex-grow: 1;
      border: 0;
      outline: none;

      font-size: 14px;
      background-color: rgba(0, 0, 0, 0);

      &::-webkit-input-placeholder {
        color: #ccc;
      }
    }

    i,
    .icon-clear {
      margin: 0 10px;
      font-size: 13px;
      color: #ccc;
    }

    .icon-clear {
      width: 14px;
      height: 14px;
      border-radius: 50%;

      background: url("../../images/icon-clear.svg") no-repeat;
      background-size: contain;
      color: rgb(235, 236, 235);
    }
  }
}

.without_res {
  font-size: 20px;
  text-shadow: 1px 1px 2px rgb(175, 175, 175);
  line-height: 60px;
  text-align: center;

  color: #bbb;
}
</style>